package controllers;

import java.awt.event.ActionEvent;
import java.util.Vector;

import models.InStoreReturnModel;

import dialogs.InsertTupleDialog;
import dialogs.ProcessTransactionDialog;

import system.View;

public class InStoreReturnController extends AbstractController {
	
	private int receiptId = -1;
	private String sname = "";
	private Vector<Vector<String>> full_return = new Vector<Vector<String>>();
	private InStoreReturnModel in_store_return = new InStoreReturnModel();

	public InStoreReturnController(View v) {
		super(v);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("Process Return"))
			//showDialogWindow(new InStoreReturnDialog(view, this));
			showDialogWindow(new InsertTupleDialog(view, "Return Item", "Transaction Info", 
					new String[] {"Receiptd ID: ", "Store Name: "}, this));
	}

	@Override
	public int validateInsert(Vector<String> s) {
		if (s.size() != 2) return VALIDATIONERROR;
		
		if (receiptId == -1 && sname.equals(""))
		{
			if (s.get(0).trim().length() != 0)
				receiptId = Integer.valueOf(s.get(0).trim());
			else return VALIDATIONERROR;
			
			if ((sname = s.get(1).trim()).length() == 0) return VALIDATIONERROR;
			
			if (in_store_return.validateReceiptAndStore(receiptId, sname)) {
				showDialogWindow(new ProcessTransactionDialog(view, "Return Items", 
						"Item Info", new String[] {"Item UPC", "Quantity"} , this));
				return OPERATIONSUCCESS;
			}
			else {
				rollBack();
				return VALIDATIONERROR;
			}
		}
		else {
			int retCode = validateEntries(s);
			
			if (retCode == OPERATIONSUCCESS) {
				int upc;
				int quantity;
				
				for (Vector<String> single_item : full_return) {
					upc = Integer.valueOf(single_item.get(0).trim());
					quantity = Integer.valueOf(single_item.get(1).trim());
					in_store_return.processReturn(receiptId, sname, upc, quantity);
				}
				
				if (in_store_return.commit()) {
					rollBack();
					return OPERATIONSUCCESS;
				}
				else return OPERATIONFAILED;
			}
			return retCode;
		}
	}

	@Override
	public int validateEntries(Vector<String> s) {
		int upc;
		int quantity;
		try {
			//If all fields are empty, don't give an error
			//Maybe user made a mistake and added item unnecessarily
			if (s.get(0).trim().length() == 0 && s.get(1).trim().length() == 0) 
				return OPERATIONSUCCESS;
			
			if (s.get(0).length() != 0)
				upc = Integer.valueOf(s.get(0).trim());
			else return VALIDATIONERROR;
			if (s.get(1).length() != 0)
				quantity = Integer.valueOf(s.get(1).trim());
			else return VALIDATIONERROR;
			
			//Don't allow zero or negative quantity
			if (quantity < 1) return VALIDATIONERROR;
			
			//If item has already been added to basket, don't let user add it again
			for (Vector<String> entry : full_return)
				if (Integer.parseInt(entry.get(0).trim()) == upc)
					return VALIDATIONERROR;
			
			if (!in_store_return.validateReturnItem(receiptId, upc, quantity)) 
				return OPERATIONFAILED;
		} catch (NumberFormatException ex) {
			return OPERATIONFAILED;
		}
		full_return.add(s);
		return OPERATIONSUCCESS;
	}
	
	@Override
	public void rollBack() {
		full_return.clear();
		receiptId = -1;
		sname = "";
	}
	
	@Override
	public void show() {
		// TODO Auto-generated method stub
	}

	@Override
	public int validateDelete(Vector<String> s) {
		// TODO Auto-generated method stub
		return 0;
	}
}